"""Custom Django template filters.
"""
import unittest

from google.appengine.ext.webapp import template

register = template.create_template_register()

@register.filter(name="hex")
def dectohex(i):
  """Converts an integer to a hexadecimal string.
  """
  try:
    return "%04x" % int(i)
  except TypeError:
    return i
 
class CustomFiltersTestCase(unittest.TestCase):
  def test(self):
    self.assertEqual("0000", dectohex(0))
    self.assertEqual("0002", dectohex(2))
    self.assertEqual("000a", dectohex(10))
    self.assertEqual("000f", dectohex(15))
    self.assertEqual("0010", dectohex(16))
    self.assertEqual("0020", dectohex(32))
    self.assertEqual("0040", dectohex(64))
    self.assertEqual("0000", dectohex("0"))
    self.assertEqual("0002", dectohex("2"))
    self.assertEqual("000a", dectohex("10"))
    self.assertEqual("000f", dectohex("15"))
    self.assertEqual("0010", dectohex("16"))
    self.assertEqual("0020", dectohex("32"))
    self.assertEqual("0040", dectohex("64"))
